#pragma once
#include <iostream>
#include <unistd.h>
using namespace std;

typedef void(*work_t)(int);
typedef void(*task_t)(int,pid_t);

void PrintLog(int fd,pid_t id)
{
    cout << "sub procss: " << id;
    cout << ", fd " << fd;
    cout << ",task is: Print Log task\n" <<endl;
}

void ReloadConf(int fd,pid_t id)
{
    cout << "sub procss: " << id;
    cout << ", fd " << fd;
    cout << ",task is: Reload Conf task\n" <<endl;
}

void ConnectMysql(int fd,pid_t id)
{
    cout << "sub procss: " << id;
    cout << ", fd " << fd;
    cout << ",task is: Connect Mysql task\n" <<endl;
}

task_t tasks[3] = {PrintLog, ReloadConf, ConnectMysql}; 

uint32_t NextTask()
{
    return rand() % 3;
}

void worker(int fd)
{
    while(1)
    {
        uint32_t command_code = 0;
        ssize_t n = read(0,&command_code,sizeof(command_code));
        if(n == sizeof(command_code))
        {
            if(command_code >= 3)continue;
            tasks[command_code](fd,getpid());
        }
        else if( n == 0 )
        {
            cout << "sub process: " << getpid() << "quit now..." << endl;
            break;
        }
        // cout << "I am worker:" << getpid() <<endl;
        // sleep(1);
    }
}


